﻿using Direct.IDAL.Message;
using DirectSell.Common.Filter;
using DirectSell.Common.Page;
using DirectSell.Common.Session;
using DirectSell.Common.Url;
using DirectSell.Model.Message;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace DirectSell.WebLayer.Areas.User.Controllers
{
    /// <summary>
    /// 留言
    /// </summary>
    public class MessageController : Controller
    {

        #region 注入
        [Dependency]
        protected IMessageRepository _mysqlRepository { get; set; }
        [Dependency]
        protected IMongoDAL.User.IMessageRepository _mongoRepository { get; set; }
        #endregion

        #region 首页
        [LoginFilter(IsCheck =true)]
        // GET: User/Message
        public async Task<ActionResult> Index(int id=1, int pageIndex = 1)
        {
            var sessionmodel = SessionInfo.GetSession();

            PageMessages pagedlist = new PageMessages();

            pagedlist.Messages_1 = await _mongoRepository.AlreadyMessage(sessionmodel.UserCode, pageIndex, CommonConfig.PageSize);

            pagedlist.Messages_2 = await _mongoRepository.MyMessage(sessionmodel.UserCode, id, CommonConfig.PageSize);

            return View(pagedlist);
        }
        #endregion

        #region 发起留言
        /// <summary>
        /// 发起留言
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> SendMessage(MessageReq req)
        {
            SessionModel session = SessionInfo.GetSession();
            var info = _mysqlRepository.GetUserInfo(session.UserCode);
            req.SendName = info.UserName;
            req.SendCode = info.UserCode;
            req.SendPhone = info.Phone;
            //写入MongoDB
            if (! await _mongoRepository.InsertMessage(req))
            {
                return Json("留言失败");
            }
            return Json("留言成功");
        }
        #endregion

        #region 留言页面
        /// <summary>
        /// 留言页面
        /// </summary>
        /// <returns></returns>
        public ActionResult MessageView()
        {
            return PartialView();
        }
        #endregion

        #region 删除一条留言
        /// <summary>
        /// 删除一条留言
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JsonResult> DeleteMessage(string id)
        {
            if (! await _mongoRepository.DeleteMessage(id))
            {
                return Json("删除失败");
            }
            return Json("删除成功");
        }
        #endregion

        #region 查看留言信息
        /// <summary>
        /// 查看留言信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> ShowMsg(string id)
        {
            var info = await _mongoRepository.QueryMsg(id);
            return PartialView(info);
        }
        #endregion

        #region 标为已读
        /// <summary>
        /// 标为已读
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JsonResult> AlRead(string id)
        {
            if (! await _mongoRepository.AlRead(id))
            {
                return Json("操作失败", JsonRequestBehavior.AllowGet);
            }
            return Json("成功", JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 分部视图
        /// <summary>
        /// 分部视图
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public async Task<ActionResult> List(int pageIndex = 1)
        {
            var session = SessionInfo.GetSession();

            var pagedList = await _mongoRepository.AlreadyMessage(session.UserCode, pageIndex, CommonConfig.PageSize);

            return PartialView(pagedList);
        }
        #endregion

        #region 分布视图1
        public async Task<ActionResult> List_1(int id = 1)
        {
            var session = SessionInfo.GetSession();

            var pagedList = await _mongoRepository.MyMessage(session.UserCode, id, CommonConfig.PageSize);

            return PartialView(pagedList);
        }
        #endregion


    }
}